package org.yjhking.delay;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 延迟消息
 *
 * @author YJH
 */
public class Producer {
    
    //演示消息同步发送
    public static void main(String[] args) throws InterruptedException, RemotingException, MQClientException, MQBrokerException, UnsupportedEncodingException {
        //生产者
        DefaultMQProducer producer = new DefaultMQProducer("syn-producerGroup-delay");
        //设置name server地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //启动
        producer.start();
        for (long i = 0; i < 4; i++) {
            Message message = new Message("topic-order-delay", "product-order-delay", "延迟消息".getBytes(RemotingHelper.DEFAULT_CHARSET));
            //延迟级别 3，代表 10s延迟
            message.setDelayTimeLevel(3);
            message.setKeys("key-" + i);
            //执行发送
            SendResult result = producer.send(message);
            System.out.println("发送时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            System.out.println(result);
        }
        producer.shutdown();
    }
}